package com.magic.zhixin.utils;

import com.magic.zhixin.config.Constant;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.Map;
import java.util.Set;


@Slf4j
public class OkHttpUtil {





    public static String post(String url,Map param){
        return post_(url,param,null);
    }

    private static String post_(String url,Map param,String authorization){
        try {

            OkHttpClient client = new OkHttpClient();//创建OkHttpClient对象。
            FormBody.Builder formBody = new FormBody.Builder();//创建表单请求体
            Set<String> keys = param.keySet();
            for(String key: keys){
                formBody.add(key,param.get(key).toString());
            }
            Request request;
            if(authorization == null){
                request = new Request.Builder()//创建Request 对象。
                        .url(url)
                        .post(formBody.build())//传递请求体
                        .build();
            }else{
                request = new Request.Builder()//创建Request 对象。
                        .url(url)
                        .header("Authorization",authorization)
                        .post(formBody.build())//传递请求体
                        .build();
            }
            Response response = client.newCall(request).execute();//得到Response 对象
            return getResult(response);
        } catch (Exception e) {
            log.error(e.toString());
           return null;
        }
    }




    public static String get(String url,Map param){
        return get_(url,param,null);
    }
    public static String getAut(String url,Map param,String authorization){
        return get_(url,param,authorization);
    }

    private static String get_(String url,Map param,String Authorization){
        try {

            OkHttpClient client = new OkHttpClient();//创建OkHttpClient对象。
            if(param != null && param.size() >1){
                Set<String> keys = param.keySet();
                url += "?";
                for(String key: keys){
                    if(!url.endsWith("?")){
                        url = url +"&";
                    }
                    url = url + key +"=" + param.get(key);
                }
            }
            Request request ;
            log.info(url);
            if(StringUtils.isEmpty(Authorization)){
                request = new Request.Builder()
                        .url(url)//请求接口。如果需要传参拼接到接口后面。
                        .build();//创建Request 对象
            }else {
                 request = new Request.Builder()
                        .url(url)//请求接口。如果需要传参拼接到接口后面。
                        .header("Authorization",Authorization)
                        .build();//创建Request 对象

            }
            Response response  = client.newCall(request).execute();//得到Response 对象
            return getResult(response);
        } catch (Exception e) {
            log.error(e.toString());
            return null;
        }
    }

    private static String getResult(Response response) {
        try {
            if (response.isSuccessful()) {
                ResponseBody body = response.body();
                String result = body.string();
                log.info(result);
                return result;
            }else{
                response.close();
            }
        } catch (IOException e) {
            log.error(e.toString());
        }
        return null;
    }

    public static  String postJson(String url,Map param) throws IOException ,Exception{
        String json =Constant.mapper.writeValueAsString(param);
        OkHttpClient client = new OkHttpClient();
        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Response response = client.newCall(request).execute();
        if (response.isSuccessful()) {
            String result =response.body().string();
            log.info(result);
            return result;
        } else {
            log.error(response.toString());
            throw new IOException("Unexpected code " + response);
        }

    }
    public static  String putJson(String url,Map param) throws IOException ,Exception{
        String json =Constant.mapper.writeValueAsString(param);
        OkHttpClient client = new OkHttpClient();
        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .put(body)
                .build();
        Response response = client.newCall(request).execute();
        if (response.isSuccessful()) {
            String result =response.body().string();
            log.info(result);
            return result;
        } else {
            log.error(response.toString());
            throw new IOException("Unexpected code " + response);
        }

    }


    public static  String deleteJson(String url,Map param) throws IOException ,Exception{
        String json =Constant.mapper.writeValueAsString(param);
        OkHttpClient client = new OkHttpClient();
        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .delete(body)
                .build();
        Response response = client.newCall(request).execute();
        if (response.isSuccessful()) {
            String result =response.body().string();
            log.info(result);
            return result;
        } else {
            log.error(response.toString());
            throw new IOException("Unexpected code " + response);
        }

    }

}
